package com.haixee.parkdemo.auth.service.impl;


import com.haixee.parkdemo.auth.dao.UserCustomMapper;
import com.haixee.parkdemo.auth.dao.UserMapper;
import com.haixee.parkdemo.auth.entity.dto.AdminLoginDTO;
import com.haixee.parkdemo.auth.entity.dto.RoleDTO;
import com.haixee.parkdemo.auth.enums.AuthScope;
import com.haixee.parkdemo.auth.enums.Resp;
import com.haixee.parkdemo.auth.enums.RoleType;
import com.haixee.parkdemo.auth.enums.UserType;
import com.haixee.parkdemo.auth.jwt.JwsTokenExtra;
import com.haixee.parkdemo.auth.jwt.JwtManager;
import com.haixee.parkdemo.auth.model.Role;
import com.haixee.parkdemo.auth.model.User;
import com.haixee.parkdemo.auth.service.AdminService;
import com.haixee.parkdemo.auth.service.TenantService;
import com.haixee.parkdemo.auth.utils.PasswordUtil;
import com.haixee.parkdemo.common.exception.BusinessException;
import com.haixee.parkdemo.common.response.CommonResEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private UserCustomMapper userCustomMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtManager jwtManager;

    @Override
    public AdminLoginDTO getAdminLogin(String username, String password) {

        User user = userCustomMapper.selectByUsername(username);
        if (user == null) {
            log.error("[后台登录],用户不存在,username:[{}]", username);
            throw new BusinessException(Resp.USER_OR_PASSWORD_ERROR);
        }

        if (!PasswordUtil.validate(password, user.getPassword())) {
            log.error("[后台登录],用户密码错误,username:[{}]", username);
            throw new BusinessException(Resp.USER_OR_PASSWORD_ERROR);
        }

        // 查询全部角色
        List<RoleDTO> roles = userCustomMapper.selectRoleDTOByTenantId(user.getId(), null);
        if (roles.size() == 0) {
            log.error("[用户未分配角色],uid:[{}]", user.getId());
            throw new BusinessException(Resp.USER_NOT_ROLE);
        }

        AdminLoginDTO dto = new AdminLoginDTO();
        dto.setUid(user.getId());
        dto.setName(user.getUsername());

        dto.setRoleList(roles);
        if (roles.size() == 1) {
            RoleDTO curRole = roles.get(0);

            JwsTokenExtra extra = new JwsTokenExtra().setTenantId(curRole.getTenantId()).setRoleId(curRole.getId()).setOrganizeId(curRole.getOrganizeId()).setRoleType(curRole.getType());
            String token = jwtManager.getOne().generateJwtWithExtra(user.getId(), AuthScope.ADMIN,  extra);
            dto.setToken(token);
        } else {
            String token = jwtManager.getOne().generateJwtWithExtra(user.getId(), AuthScope.ADMIN, new JwsTokenExtra());
            dto.setToken(token);
        }

        return dto;
    }

    @Override
    public AdminLoginDTO checkout(Integer uid, Integer roleId) {
        // 查询全部角色
        List<RoleDTO> roles = userCustomMapper.selectRoleDTOByTenantId(uid, null);
        if (roles.size() == 0) {
            log.error("[用户未分配角色],uid:[{}]", uid);
            throw new BusinessException(Resp.USER_NOT_ROLE);
        }

        RoleDTO curRole = null;
        for (RoleDTO role : roles) {
            if (role.getId().equals(roleId)) {
                curRole = role;
                break;
            }
        }

        if (curRole == null) {
            throw new BusinessException(CommonResEnum.BAD_REQUEST);
        }

        User user = userMapper.selectByPrimaryKey(uid);
        AdminLoginDTO dto = new AdminLoginDTO();
        dto.setUid(user.getId());
        dto.setName(user.getUsername());
        dto.setRoleList(roles);
        dto.setCurRole(curRole);

        JwsTokenExtra extra = new JwsTokenExtra().setTenantId(curRole.getTenantId()).setRoleId(curRole.getId()).setOrganizeId(curRole.getOrganizeId()).setRoleType(curRole.getType());
        String token = jwtManager.getOne().generateJwtWithExtra(user.getId(), AuthScope.ADMIN,  extra);
        dto.setToken(token);


        return dto;
    }

    public static void main(String[] args) {
        System.out.println(PasswordUtil.encode("1"));
    }
}
